package com.example.myfirst.controller;

import com.example.myfirst.entity.ShopCart;
import com.example.myfirst.service.ShopCartService;
import com.example.myfirst.vo.ResultVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shopcart")
@CrossOrigin
public class ShopCartController {
    @Autowired
    private ShopCartService shopCartService;

    @ApiOperation("查询商品是否已在购物车中")
    @GetMapping("/one")
    public ResultVo findGoods(@RequestParam Integer userId, @RequestParam Integer goodsId) {
        ShopCart goods = shopCartService.findGoodsByUserIdAndGoodsId(userId, goodsId);
        ResultVo rv = new ResultVo();
        if (goods != null) {
            rv.setSuccess(true);
            rv.setMessage("购物车中已存在该商品");
        } else {
            rv.setSuccess(false);
            rv.setMessage("购物车中不存在该商品");
        }
        rv.setCode(200);
        return rv;
    }

    @ApiOperation("添加购物车")
    @PostMapping("/add")
    public ResultVo addShopCart(@RequestBody Map<String, Integer> map) {
        Integer userId = map.get("userId");
        Integer goodsId = map.get("goodsId");
        Boolean flag = shopCartService.addShopCart(userId, goodsId);
        ResultVo rv = new ResultVo();
        if (flag) {
            rv.setSuccess(true);
            rv.setMessage("商品添加购物车成功");
        } else {
            rv.setSuccess(false);
            rv.setMessage("商品添加购物车失败~");
        }
        rv.setCode(200);
        return rv;
    }

    @ApiOperation("根据用户id显示购物车数据")
    @GetMapping("/list")
    public ResultVo getShopCartById(@RequestParam Integer userId) {
        List<ShopCart> shopCarts = shopCartService.queryShopCartByUserId(userId);
        return new ResultVo(shopCarts);
    }

    @ApiOperation("根据用户id和商品id查找商品,查找到了再对数量进行加一操作")
    @PutMapping("/update")
    public ResultVo updateShopCart(@RequestBody Map<String, Integer> map) {
        Integer userId = map.get("userId");
        Integer goodsId = map.get("goodsId");
        Boolean flag = shopCartService.updateShopCart(userId, goodsId);
        ResultVo rv = new ResultVo();
        if (flag) {
            rv.setSuccess(true);
            rv.setMessage("购物车数量更新成功");
        } else {
            rv.setSuccess(false);
            rv.setMessage("购物车数量更新失败~");
        }
        rv.setCode(200);
        return rv;
    }

    @ApiOperation("根据用户id和商品id查找商品,查找到了再对数量进行赋值操作")
    @PutMapping("/updateNum")
    public ResultVo updateShopCartNum(@RequestBody Map<String, Integer> map) {
        Integer userId = map.get("userId");
        Integer goodsId = map.get("goodsId");
        Integer num = map.get("num");
        Boolean flag = shopCartService.updateShopCart(userId, goodsId, num);
        ResultVo rv = new ResultVo();
        if (flag) {
            rv.setSuccess(true);
            rv.setMessage("购物车数量更新成功");
        } else {
            rv.setSuccess(false);
            rv.setMessage("购物车数量更新失败~");
        }
        rv.setCode(200);
        return rv;
    }

    @ApiOperation("根据用户id和商品id删除商品")
    @DeleteMapping("/delete")
    public ResultVo deleteGoods(@RequestParam Integer userId, @RequestParam Integer goodsId) {
        Boolean flag = shopCartService.delShopCart(userId, goodsId);
        ResultVo rv = new ResultVo();
        if (flag) {
            rv.setSuccess(true);
            rv.setMessage("删除购物车商品成功");
        } else {
            rv.setSuccess(false);
            rv.setMessage("删除购物车商品失败~");
        }
        rv.setCode(200);
        return rv;
    }
}
